var engineList:Object;

function startEngine(channel:String)
{
	 Packages.log("startEngine...", "debug");
	if (engineList[channel].engine==null || engineList[channel].engine.status=="idle") 
	{
		mlf.mcmanager.MessageAgent.getMessageDispatcher().addEventListener("onSync", this);
		var nc=Packages.services.RTMPConn.getInstance();
		if (nc!=null)
		{
			Packages.log("nc existed..."+nc.connected, "info");
			
			// var engine=mlf.gatekeeper.native.Caller.collabEngine();
			var engineObj:Object=new Object();
			engineObj.engine=mlf.gatekeeper.native.Caller.collabEngine();
			engineObj.engine.start(nc, channel);
			engineObj.channel=channel;
			if (engineList==null) engineList=new Object();
			engineList[channel]=engineObj;
		}
	}
}

function stopEngine(channel:String)
{
	if (engineList[channel].engine.status=="process") 
	{
		engineList[channel].engine.stop();
		engineList[channel].user="";
		engineList[channel]=null;
		delete engineList[channel];
		mlf.mcmanager.MessageAgent.getMessageDispatcher().removeEventListener("onSync", this);
	}
}

function onSync(evtObj:Object)
{
	// Packages.log("onSync...", "debug");
	registerTmp(evtObj.channel);
	
	if (evtObj.list!=null) 
	{
		var listInfo=new Object();
		listInfo.type="onEvtListInfo";
		listInfo.channel=evtObj.channel;
		listInfo.param=evtObj.list;
		mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch(listInfo);
	}
	if (evtObj.param!=null)
	{
		for (var i=0; i<evtObj.param.length; i++)
		{
			if (evtObj.param[i].type=="statement")
			{
				initStatement(evtObj.param[i].slot, evtObj.channel);
			}
			else 
			{
				var target=mlf.mcmanager.PathResolver.extractPath(evtObj.param[i].path);
				if (target!=null) mlf.gatekeeper.native.Caller.callee(target, evtObj.param[i].func, evtObj.param[i].args);
			}
		}
	}
}

function registerTmp(channel:String)
{
	if (engineList[channel].user=="" || engineList[channel].user==null) 
	{	
		if (Packages.model.UserProfile.data.nickName!=null) register(Packages.model.UserProfile.data.nickName, channel);
	}
}

function initStatement(target, channel)
{
	// var stateMessage:Array=new Array();
	/* stateMessage.push(Packages.task.Composition.dataProvider());
	 stateMessage.push(Packages.task.Composition.AudioDataProvider());
	if (currentUser=="collab_admin")
	{
		var args=new Array();
		args.push(Packages.task.Composition.audioId);
		stateMessage.push(Packages.system.Collab.wrapInfo(this, "acceptAdmin", args));
	} 
	// var stateMessage:Array=new Array();
	// stateMessage.push("");
	// trace("initStatement.target: "+target);*/
	var stateMessage:Array=new Array();
	stateMessage.push("");
	engineList[channel].engine.update(stateMessage, target);
}

function updateInfo(ins, func, args, channel)
{
	var actionMessage=wrapInfo(ins, func, args);
	engineList[channel].engine.update(actionMessage);
}

function wrapInfo(ins, func, args):Object
{
	var mess=new Object();
	mess.path=mlf.mcmanager.PathResolver.drawPath(ins);
	mess.func=func;
	mess.args=args;
	return mess;
}

function register(slotName:String, channel:String)
{
	// Packages.log("register...", "debug");
	engineList[channel].engine.register(slotName);
	engineList[channel].user=slotName;
}